package com.acwing.partition12;

import java.io.*;

/**
 * @author `RKC`
 * @date 2022/1/19 9:26
 */
public class AC1146新的开始 {

    private static final int N = 310, INF = 0x3f3f3f3f;
    private static int[][] weight = new int[N][N];
    private static int[] dist = new int[N];
    private static boolean[] visited = new boolean[N];
    private static int n = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        //建立一个超级源点0，所有点自建发电站的花费就转换成了超级源点到节点的电网花费
        n = Integer.parseInt(reader.readLine());
        for (int i = 1; i <= n; i++) weight[i][0] = weight[0][i] = Integer.parseInt(reader.readLine());
        for (int i = 1; i <= n; i++) {
            String[] s = reader.readLine().split("\\s+");
            for (int j = 1; j <= n; j++) {
                weight[i][j] = Integer.parseInt(s[j - 1]);
            }
        }
        writer.write(prim() + "\n");
        writer.flush();
    }

    private static int prim() {
        int answer = 0;
        for (int i = 0; i < N; i++) dist[i] = INF;
        dist[0] = 0;
        for (int k = 0; k <= n; k++) {
            int u = -1;
            for (int i = 0; i <= n; i++) {
                if (!visited[i] && (u == -1 || dist[i] < dist[u])) u = i;
            }
            if (u == -1) return INF;
            visited[u] = true;
            answer += dist[u];
            for (int v = 0; v <= n; v++) dist[v] = Math.min(dist[v], weight[u][v]);
        }
        return answer;
    }
}
